package com.xzh.homeliving.commodity.service.impl;

import com.xzh.homeliving.commodity.entity.AtEntity;
import com.xzh.homeliving.commodity.service.AtService;
import com.xzh.homeliving.commodity.vo.AttrGroupWithAttrsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xzh.common.utils.PageUtils;
import com.xzh.common.utils.Query;

import com.xzh.homeliving.commodity.dao.AttrDao;
import com.xzh.homeliving.commodity.entity.AttrEntity;
import com.xzh.homeliving.commodity.service.AttrService;

import javax.annotation.Resource;


/**
 * @author xzh
 */
@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
    @Resource
    private AtService atService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {
        // 先得到检索的关键字
        String key = (String)params.get("key");
        // 创建一个wrapper对象，并且将查询条件封装到其中
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        // 如果key值存在，就将其加入到查询条件中，key对应查询的关键字
        if(StringUtils.isNotBlank(key)) {
            wrapper.and((obj) -> {
                obj.eq("id", key).or().like("name", key);
            });
        }
        // 如果categoryId存在，就将其加入到查询条件中，categoryId对应树型列表的属性ID
        if(categoryId != 0) {
            wrapper.eq("category_id", categoryId);
        }
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrs(Long categoryId) {
        List<AttrEntity> attrEntityList = this.list(new QueryWrapper<AttrEntity>().eq("category_id", categoryId));
        return attrEntityList.stream().map(attrGroupEntity -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(attrGroupEntity, attrGroupWithAttrsVo);
            List<AtEntity> relationAttr = atService.getRelationAttr(attrGroupEntity.getId());
            if(relationAttr == null) {
                attrGroupWithAttrsVo.setAttrs(new ArrayList<>());
            } else {
                attrGroupWithAttrsVo.setAttrs(relationAttr);
            }
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());
    }
}